import { useParams } from "react-router-dom";
import { getQuestion } from "../service";
import { useDispatch } from "react-redux";
import { resetComponents } from "../store/ComponentReducer";
import { setQuestion ,changeTitleQuestion} from "../store/QuestionReducer";
import { useRequest } from "ahooks";
import { useEffect } from "react";

const useLoadQuestion = ()=>{
    const {id=''} = useParams()
    const dispatch = useDispatch()
    const {run,loading} = useRequest(()=>{
        return getQuestion(id?.toString())
    },{
        manual:true,
        onSuccess(res){
            const {title,desc,jscode,csscode,list} = res
            const selectedId = res.list[0].fe_id
            dispatch(setQuestion({
                title,
                desc,
                jscode,
                csscode
            }))
            dispatch(resetComponents({
                selectedId,
                componentList:list,
                copyComponent:null
            }))
        }
    })
    useEffect(()=>{
        run()
    },[id])
    return {
        loading
    }
}

export default useLoadQuestion